<%--
  Created by IntelliJ IDEA.
  User: LJH
  Date: 2020/2/8
  Time: 10:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>主页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/public.css" media="all" />
</head>
<body>
<h1 align="center">账单列表</h1>
<hr>
<h3 align="right" style="margin-right: 50px; color:teal;">
    你好:${user.name}
</h3>
<hr>
<div>
    <form class="layui-form" id="searchFrm" method="post" align="center">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">账单类型</label>
                <div class="layui-input-inline">
                    <select id="typeid" name="typeid">
                        <option value="">请选择账单类型</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="startTime" id="startTime"  placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="endTime" id="endTime"  placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSerach" id="doSearch">查询</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button type="button" class="layui-btn layui-btn-warm" id="openAddBills">添加</button>
            </div>
        </div>
    </form>
</div>
<hr>
<div>
    <table id="billsTable" lay-filter="billsTable"></table>
</div>

<%--添加弹出层的内容--%>
<div id="addBillsDiv" style="display: none;margin: 5px;">
    <form method="post" id="dataFrm" class="layui-form" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">账单类型</label>
            <div class="layui-input-block" id="dataFrmBillsType">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账单标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="billstime" id="billstime" placeholder="请选择日期" readonly  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">金额</label>
                <div class="layui-input-inline">
                    <input type="number" name="price" lay-verify="required" placeholder="请输入金额" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remark" placeholder="请输入备注" ></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<div></div>
<script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>

<script>
    layui.use(['form','layer','jquery','laydate','table'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;
        var laydate=layui.laydate;
        var table=layui.table;
        //渲染日期选择器
        laydate.render({
            elem:'#startTime',
            type: 'date'
        });
        laydate.render({
            elem:'#endTime',
            type: 'date'
        });
        laydate.render({
            elem:'#billstime',
            type: 'date'
        });


        //加载 查询的下拉框账单类型的数据
        $.get("${pageContext.request.contextPath}/billstype/queryAllBillstype.do",function(res){
            var searchSelect=$("#typeid");
            var html="<option value=''>请选择账单类型</option>";
            $.each(res,function(i,item){

                html+= "<option value="+item.id+">"+item.name+"</option>";
            })
            searchSelect.html(html);
            form.render();
        });
        //加载 添加的下拉框账单类型的数据
        $.get("${pageContext.request.contextPath}/billstype/queryAllBillstype.do",function(res){
            var searchRadio =$("#dataFrmBillsType");
            var html="";
            $.each(res,function(i,item){
                if(i==0) {
                    html += '<input type=radio name=typeid value=' + item.id + ' checked  title=' + item.name + '>';
                }else {
                    html += '<input type=radio name=typeid value=' + item.id + ' title=' + item.name + '>';
                }
            });
            searchRadio.html(html);
            form.render();
        });



       var tableIns = table.render({
            elem: '#billsTable'
            ,url:'${pageContext.request.contextPath}/bills/queryAllBills.do'
            ,cols: [[
                {field:'id',  title: 'ID',align:"center"}
                ,{field:'title',  title: '标题',align:"center"}
                ,{field:'billstime', title: '账单时间',align:"center"}
                ,{field:'typename',  title: '账单类型',align:"center"}
                ,{field:'price', title: '金额', align:"center"}
                ,{field:'remark',  title: '备注',align:"center"}
            ]]
            ,page: true
        });

       //模糊查询
        form.on("submit(doSerach)",function (data) {
            var params=$("#searchFrm").serialize();
            tableIns.reload({
                url:"${pageContext.request.contextPath}/bills/queryAllBills.do?"+params
            })
            return false;
        });


        //打开添加页岩
        var mainIndex;
        $("#openAddBills").click(function () {
            mainIndex=  layer.open({
                type: 1,
                title: "添加账单",
                content: $("#addBillsDiv"),
                area: ['800px','600px']
            })
        })

        //保存
        form.on("submit(doSubmit)",function()
        {
            var params=$("#dataFrm").serialize();
           $.post("${pageContext.request.contextPath}/bills/addBills.do",params,function(res) {
               if(res.code==200){
                   tableIns.reload();
               }
               layer.msg(res.msg);
               layer.confirm('是否继续添加？', {
                   btn: ['继续', '取消'] //可以无限个按钮
               }, function(index, layero){
                   $("#dataFrm")[0].reset();
                   layer.close(index);
                   return false;
               }, function(index){
                   layer.close(index);
                   layer.close(mainIndex);
               });
           });
            return false;
           });
    });

</script>

</body>
</html>